//
//  EPSheetViewController.swift
//  EPHome
//
//  Created by liujianlong on 2022/9/5.
//

import UIKit

class EPSheetViewController: EPAlertViewController {
    var tableView : UITableView?
    var block:((_ index:Int, _ title: String)->Void)?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.initUI()
    }
    override func initUI() {
        self.tableView = UITableView(frame: .zero, style: .plain)
        self.tableView?.separatorStyle = .none
        self.tableView?.showsVerticalScrollIndicator = false
        self.tableView?.showsHorizontalScrollIndicator = false
        self.tableView?.backgroundColor = .clear
        self.tableView?.bounces = false
        self.tableView?.delegate = self
        self.tableView?.dataSource = self
        self.tableView?.register(EPSheetCell.nib(), forCellReuseIdentifier: EPSheetCell.ID())
        if #available(iOS 11.0, *) {
            self.tableView!.contentInsetAdjustmentBehavior = .never
        } else {
            self.automaticallyAdjustsScrollViewInsets = false
        }
        self.view.addSubview(self.tableView!)
    }
    
    var array = [String](){
        didSet{
            self.tableView?.snp.makeConstraints({ make in
                make.height.equalTo(56 * array.count + 56 + 8)
                make.left.right.equalToSuperview().inset(20)
                make.bottom.equalToSuperview().inset(30)
            })
            self.tableView?.layoutIfNeeded()
            self.tableView?.reloadData()
        }
    }
}
//MARK: - UITableViewDataSource
extension EPSheetViewController:UITableViewDataSource
{
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return section == 0 ? self.array.count : 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: EPSheetCell.ID()) as! EPSheetCell
        if indexPath.section == 0 && self.array.count > 0{
            cell.line.isHidden = (indexPath.row == self.array.count - 1)
            cell.title.text = LS(self.array[indexPath.row])
        }else{
            cell.line.isHidden = true
            cell.title.text = LS("取消")
        }
        
        return cell
    }
}
//MARK: - UITableViewDelegate
extension EPSheetViewController:UITableViewDelegate{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.dismiss(animated: false)
        if indexPath.section == 0{
            self.block?(indexPath.row, self.array[indexPath.row])
        }
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return section == 0 ? 8 : 0
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        if section == 0{
            let view = UIView()
            view.backgroundColor = .clear
            return view
        }
        return nil
    }
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        var path : UIBezierPath?
        if indexPath.section == 0 && (indexPath.row == 0 || indexPath.row == self.array.count - 1){
            if indexPath.row == 0{
                path = UIBezierPath(roundedRect:cell.bounds, byRoundingCorners: [.topLeft,.topRight], cornerRadii: CGSize(width: 4, height: 4))
            }else{
                path = UIBezierPath(roundedRect:cell.bounds, byRoundingCorners: [.bottomLeft,.bottomRight], cornerRadii: CGSize(width: 4, height: 4))
            }
            let layer = CAShapeLayer()
            layer.frame = cell.bounds
            layer.path = path?.cgPath
            cell.layer.mask = layer
        }else if indexPath.section == 1{
            path = UIBezierPath(roundedRect:cell.bounds, byRoundingCorners: [.topLeft,.topRight,.bottomLeft,.bottomRight], cornerRadii: CGSize(width: 4, height: 4))
            let layer = CAShapeLayer()
            layer.frame = cell.bounds
            layer.path = path?.cgPath
            cell.layer.mask = layer
        }else{
            cell.layer.mask = nil
        }
    }
}
